package org.daochong.web.filter;

import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;

public class ResponseHeaderFilter implements Filter {
	private Map<String, String> headers = new HashMap<String, String>();

	public void destroy() {
	}

	public void doFilter(ServletRequest req, ServletResponse resp, FilterChain filterChain)
			throws IOException, ServletException {
		HttpServletResponse response = (HttpServletResponse) resp;
		for (String key : this.headers.keySet()) {
			response.setHeader(key, (String) this.headers.get(key));
		}
		filterChain.doFilter(req, resp);
	}

	public void init(FilterConfig config) throws ServletException {
		Enumeration<?> en = config.getInitParameterNames();
		while (en.hasMoreElements()) {
			String key = (String) en.nextElement();
			this.headers.put(key, config.getInitParameter(key));
		}
	}
}